'use strict';

import * as moment from 'moment'
import utils from 'src/core/utils';
import mongodbHandle from 'src/handle/mongodbHandle';
import CountersDao from './counterDao';
import { pid } from 'process';

class CheatDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("cheat")
        this.handle.createIndex("pID", 1)
        this.handle.createIndex("uID", 1)
    }

    newAndSave = async (uID, pID, beforeDiamond, beforeGold, diamond, gold): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            uID: uID,
            pID: pID,
            beforeDiamond,
            diamond,
            beforeGold,
            gold,
            createTime: date,
        });
    }

    newAndSave2 = async (uID, pID, beforePower, beforeFloor, power, floor): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            uID: uID,
            pID: pID,
            beforePower,
            power,
            beforeFloor,
            floor,
            createTime: date,
        });
    }

    newAndSave3 = async (uID, pID, power, floor): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            uID: uID,
            pID: pID,
            power,
            floor,
            createTime: date,
        });
    }

    newAndSave4 = async (uID, pID, townLvl ,booming, usedItem, needUsedItem): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            uID: uID,
            pID: pID,
            booming,
            usedItem,
            townLvl,
            needUsedItem,
            createTime: date,
        });
    }

    newAndSave5 = async (uID, pID, itemType, itemCount): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            uID: uID,
            pID: pID,
            itemType,
            itemCount,
            createTime: date,
        });
    }

    newAndSave6 = async (pID, itemType, itemCount): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            pID: pID,
            itemType,
            itemCount,
            createTime: date,
        });
    }


    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }

    findFieldByPageAndSort = async (query, sort, field, start, size): Promise<any[]> => {
        return await this.handle.find(query, field).sort(sort).skip(start).limit(size).toArray()
    }

    findAndSort = async (query, sort): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default CheatDao